<?php

class UserController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        // action body
    }

    public function registerAction()
    {
        $form = new Form_Register();
        $request = $this->getRequest();
        
        if( $request->isPost() && $form->isValid($post = $request->getPost()))
        {
        	$form->persistData($post);
        	
        	$this->_helper->flashMessenger->addMessage(array('type' => 'success', 'text' => 'Your account was created, but is inactive. Check your email and confirm your registration.'));
        	$this->_helper->redirector('index','index','default');
        }
        else {
        	$this->view->form = $form;
        }
    }

    public function loginAction()
    {
    	$form = new Form_Login();
    	$request = $this->getRequest();
    	
    	if( $request->isPost() && $form->isValid($request->getPost())){
	    	
	    	$adapter = new App_Auth_Adapter_Doctrine();
	    	$adapter->setModel('Model_User');
	    	$adapter->setIdentityColumn('email');
	    	$adapter->setCredentialColumn('password');
	    	$adapter->setIdentity($form->getValue('email'));
	    	$adapter->setCredential($form->getValue('password'));
	    	
	    	$auth = Zend_Auth::getInstance();
	    	$result = $auth->authenticate($adapter);
	    	
	    	if($result->isValid()){
	    		$this->_helper->flashMessenger->addMessage(array('type' => 'success', 'text' => 'You rae succesfully authenticated and logged in.'));
	        	$this->_helper->redirector('index','index','default');
	    	}
	    	else{
	    		switch ( $result->getCode()){
	    			case Zend_Auth_Result::FAILURE_IDENTITY_NOT_FOUND :
	    				$message = array('type' => 'error','text' => 'User doesn\'t exists');
	    				break;
	    			case Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID :
	    				$message = array('type' => 'error','text' => 'Wrong password');
	    				break;
	    			case Zend_Auth_Result::FAILURE_IDENTITY_AMBIGUOUS :
	    				$message = array('type' => 'error','text' => 'You can\'t log in. Get out!');
	    				break;
	    			default:
	    				$message = array('type' => 'error','text' => 'Strange error. Try again');
	    		}
	    	}
	    	$this->view->flash[] = $message;
    	}
    	
    	$this->view->form = $form;
    }

    public function logoutAction()
    {
        Zend_Auth::getInstance()->clearIdentity();
        $this->_helper->flashMessenger->addMessage(array('type' => 'success', 'text' => 'You are logged out :('));
        $this->_helper->redirector('index','index','default');
    }

    public function changeAction()
    {
        // action body
    }

    public function confirmAction()
    {
        if($this->_hasParam('id') && $this->_hasParam('token')
        	&& is_numeric($id = $this->_getParam('id'))
        	&& strlen($token = $this->_getParam('token')) == 32 )
        {
        	$user = Doctrine_Core::getTable('Model_User')->find($id);
        	if ($user && $user->status == 0 && $token == md5( $user->id . $user->email . $user->salt  ) )
        	{
        		$user->status = 1;
        		$user->save();
        		
        		$this->_helper->flashMessenger->addMessage(array('type' => 'success', 'text' => 'Your account is now active. Vou can sign in.'));
        		$this->_helper->redirector('login','user','default');
        	}
        	else if (!$user){
        		$this->_helper->flashMessenger->addMessage(array('type' => 'error', 'text' => 'User doesnt exists!'));
        	}
        	else if ($user->status == 1){
        		$this->_helper->flashMessenger->addMessage(array('type' => 'error', 'text' => 'Account already active!'));
        	}
        	else if ($user->status < 0){
        		$this->_helper->flashMessenger->addMessage(array('type' => 'error', 'text' => 'You are persona non grata :< Get out!'));
        	}
        }
        else {
        	$this->_helper->flashMessenger->addMessage(array('type' => 'error', 'text' => 'Invalid confirmation data!'));
        }
        
        $this->_helper->redirector('login','user','default');
    }


}